import { http } from '@/utils/http';
import type { LoginResult } from '@/types/auth';
import type { AnyObj } from '@/types/global';
import { useAuthStore } from '@/stores';
import { isLogin, getWxUserId } from '@/utils/auth';
const appId = import.meta.env.VITE_APP_ID;
const clientId = import.meta.env.VITE_CLIENT_ID;
export const getOpenId = (code : string) => {
	return http({
		method: 'POST',
		url: '/auth/getOpenId',
		data: {
			code,
			appId,
			grantType: 'miniapp',
			clientId
		}
	});
}

export const login = (openId : string, code : string) => {
	const brokerageCode = uni.getStorageSync('brokerageCode') ?? '';
	return http<LoginResult>({
		method: 'POST',
		url: '/auth/login',
		data: {
			appId,
			clientId,
			grantType: 'miniapp',
			openId,
			code,
			brokerageCode
		}
	});
}

export const logout = () => {
	return http({
		method: 'POST',
		url: '/auth/logout',
		data: {
			appId,
			clientId,
			grantType: 'miniapp',
		}
	});
}


export const updateUserAccount = (data : AnyObj) => {
	const wxUserId = getWxUserId();
	return http<LoginResult>({
		method: 'POST',
		url: '/wx/account/updateAccount',
		data: {
			...data,
			wxUserId
		}
	});
}

export const getAccountInfo = () => {
	const userId = getWxUserId();
	return http<AnyObj>({
		method: 'GET',
		url: `/wx/account/${userId}`,
		forceLogin: false
	});
}


export const saveUnfollow = (followeeId : string) => {
	const wxUserId = getWxUserId();
	return http({
		method: 'POST',
		url: `/wx/account/unfollow`,
		data: {
			wxUserId,
			followeeId
		}
	});
}

export const saveFollow = (followeeId : string) => {
	const wxUserId = getWxUserId();
	return http({
		method: 'POST',
		url: `/wx/account/follow`,
		data: {
			wxUserId,
			followeeId
		}
	});
}

export const getUserFollowList = () => {
	const wxUserId = getWxUserId();
	return http<AnyObj[]>({
		method: 'GET',
		url: `/wx/account/listFollow/${wxUserId}`
	});
}

export const getNimAccount = (openId) => {
	return http<AnyObj>({
		method: 'GET',
		url: `/wx/account/nimAccount/${openId}`
	});
}


export const getImChatUserList = () => {
	return http<AnyObj[]>({
		method: 'GET',
		url: `/wx/account/listChatUser`
	});
}